package cn.zxl.spring.holiday.job.service.impl;

import cn.zxl.spring.holiday.job.domain.Role;
import cn.zxl.spring.holiday.job.mapper.PermissionMapper;
import cn.zxl.spring.holiday.job.mapper.RoleMapper;
import cn.zxl.spring.holiday.job.qo.QueryObject;
import cn.zxl.spring.holiday.job.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bofeifei
 * @since 2021-10-01
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> roles = roleMapper.selectList(null);
        return new PageInfo<>(roles);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRolAndPer(Role role, Long[] permissionIds) {
        roleMapper.saveOne(role);
        permissionMapper.saveRolAndPer(role.getId(),permissionIds);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateRolAndPer(Role role, Long[] permissionIds) {
        roleMapper.updateById(role);
        permissionMapper.removePermissionsByRoleId(role.getId());
        permissionMapper.saveRolAndPer(role.getId(),permissionIds);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteForRole(Long id) {
        roleMapper.deleteById(id);
        permissionMapper.removePermissionsByRoleId(id);
    }

    @Override
    public List<Role> findByEmployeeId(Long id) {
        return roleMapper.findByEmployeeId(id);
    }

    @Override
    public List<String> findRolesByEid(Long id) {
        return roleMapper.findRolesByEid(id);
    }
}
